package com.server;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.spi.DirStateFactory.Result;

import acessoDados.Singleton;
import constants.ProjectConstants;
import core.Relatorio;
import core.Viagem;


class Time extends Thread {
	
	private int inicial, actual, elapsed;

	public static void main(String args[]) {
		int numero = 0;

			while (true) {
				numero++;
				Time c = new Time();
			}

	}

	public Time() {

		
			this.start();
	}

	public void run() {
		inicial = 0;//(int) (System.currentTimeMillis()/1000);
		elapsed=0;
		actual=0;

			while (true) {
				actual += ProjectConstants.TIME_BETWEEN_SLEEPS;// (int) (System.currentTimeMillis()/1000);
				
				try {
					ResultSet rs = Singleton.getInstance().getViagemDAO().getViagensAFechar(actual);
					while(rs.next()){
						Viagem v = new Viagem();
						v.setIdViagem(rs.getInt("idViagem"));
						v.setState(ProjectConstants.ACTIVO);
						v.fechaViagem();
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				
				if(elapsed>=ProjectConstants.TIME_BETWEEN_REPORTS){
					Relatorio relatorio = new Relatorio();
					relatorio.gerarRelatorio();
//					System.out.println("Elapsed: "+elapsed+" seconds.");
//					System.out.println("Relatório Gerado.");
					elapsed=0;
				}else
//					System.out.println("Elapsed: "+elapsed+" seconds.");
					
				elapsed+=ProjectConstants.TIME_BETWEEN_SLEEPS;
				try {
					Thread.sleep(ProjectConstants.TIME_BETWEEN_SLEEPS*1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		
	}

	public int getInicial() {
		return inicial;
	}

	public void setInicial(int inicial) {
		this.inicial = inicial;
	}

	public int getActual() {
		return actual;
	}

	public void setActual(int actual) {
		this.actual = actual;
	}

	public int getElapsed() {
		return elapsed;
	}

	public void setElapsed(int elapsed) {
		this.elapsed = elapsed;
	}

	
	
	
}
